package cn.iocoder.yudao.module.member.service.userbackamount;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.userbackamount.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userbackamount.UserBackAmountDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.userbackamount.UserBackAmountMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 用户提现/退款记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserBackAmountServiceImpl implements UserBackAmountService {

    @Resource
    private UserBackAmountMapper userBackAmountMapper;

    @Override
    public Long createUserBackAmount(UserBackAmountSaveReqVO createReqVO) {
        // 插入
        UserBackAmountDO userBackAmount = BeanUtils.toBean(createReqVO, UserBackAmountDO.class);
        userBackAmountMapper.insert(userBackAmount);
        // 返回
        return userBackAmount.getId();
    }

    @Override
    public void updateUserBackAmount(UserBackAmountSaveReqVO updateReqVO) {
        // 校验存在
        validateUserBackAmountExists(updateReqVO.getId());
        // 更新
        UserBackAmountDO updateObj = BeanUtils.toBean(updateReqVO, UserBackAmountDO.class);
        userBackAmountMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserBackAmount(Long id) {
        // 校验存在
        validateUserBackAmountExists(id);
        // 删除
        userBackAmountMapper.deleteById(id);
    }

    private void validateUserBackAmountExists(Long id) {
        if (userBackAmountMapper.selectById(id) == null) {
            throw exception(USER_BACK_AMOUNT_NOT_EXISTS);
        }
    }

    @Override
    public UserBackAmountDO getUserBackAmount(Long id) {
        return userBackAmountMapper.selectById(id);
    }

    @Override
    public PageResult<UserBackAmountDO> getUserBackAmountPage(UserBackAmountPageReqVO pageReqVO) {
        return userBackAmountMapper.selectPage(pageReqVO);
    }

}